package com.controller;

import com.bean.Commodity;
import com.bean.Movie;
import com.service.MovieService;
import com.util.PageAssistant;
import com.vo.MovieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Controller
@RequestMapping("movie")
public class MovieController {
    @Autowired
    private MovieService movieService;

    /**
     * 查询在播电影信息
     */
    @RequestMapping("query1")
    public String query(HttpSession session, Model model, PageAssistant<Movie> paMovie,Movie movie){
        System.out.println(">>>>>>>>> Movie query1");
        Map<String,Object> map=new HashMap<>();
        List<Movie> queryList=movieService.query(map);

        map.put("start",paMovie.getStart());
        map.put("size",paMovie.getSize());

        paMovie.setCount(movieService.count(map));
        paMovie.setItems(movieService.query(map));



        for(int i=0;i<queryList.size();i++){
            if(queryList.get(i).getState()==0){
                model.addAttribute("paMovie",paMovie);
                return "page/movie/movieList";
            }
        }
        return null;
    }

    /**
     * 查询下架电影信息
     */
    @RequestMapping("query2")
    public String query2(HttpSession session, Model model, PageAssistant<Movie> paMovie,Movie movie){
        System.out.println(">>>>>>>>> Movie query2");
        Map<String,Object> map=new HashMap<>();
        List<Movie> queryList=movieService.query(map);

        map.put("start",paMovie.getStart());
        map.put("size",paMovie.getSize());

        paMovie.setCount(movieService.count(map));
        paMovie.setItems(movieService.query(map));



        for(int i=0;i<queryList.size();i++){
            if(queryList.get(i).getState()==0){
                model.addAttribute("paMovie",paMovie);
                return "page/movie/movie";
            }
        }
        return null;
    }

    /**
     * 添加电影
     * @return
     */
    @RequestMapping("add")
    public String add() {return "page/movie/add";}

    /**
     * 添加
     * @return
     */
    @RequestMapping("save")
    public String save(HttpSession session, Model model, MovieVo movie){
        System.out.println(">>>>>> movie save");
        System.out.println("movie:"+movie);
        System.out.println("------------------");
        List<Movie> queryList=movieService.query(new HashMap<>());

        for(Movie m:queryList){
            if(movie.getMovieName().equals(m.getMovieName())){
                model.addAttribute("msg","电影已存在");
                return "page/movie/add";
            }
        }

        movie.setImage(" ");
        movie.setScore(" ");
        movie.setState("1");
        movie.setSpare(" ");
        int save=movieService.save(movie.parse());
        System.out.println("save"+save);
        if(save == 1){
            session.setAttribute("msg","添加成功");
            return "redirect:/movie/query";
        }
        System.out.println(movie);
        return null;
    }


    @RequestMapping("see")
    public String modify(Model model, Movie movie){
        System.out.println(">>>>>> movie see");
        System.out.println("movie:"+movie);
        System.out.println("------------------");
        List<Movie> movieSee =movieService.find(movie);
        if(movieSee.size()==1){
            model.addAttribute("movieSee",movieSee.get(0));
            return "page/movie/moviemodify";
        }
        return null;
    }
    /**
     * 修改电影
     */
    @RequestMapping("update")
    public String modify(HttpSession session,MovieVo movie){
        System.out.println(">>> movie update");
        System.out.println(movie);
        System.out.println("------------------------------------------------");

        int count=movieService.modify(movie.parse());

        String updateMsg="修改成功";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "修改失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "redirect:/movie/unique?id="+movie.getId();
    }
    /**
     * 删除电影
     */
    @RequestMapping("delete")
    public String delete(Movie movie){
        int remove=movieService.remove(movie);

        if(remove ==1){
            return "redirect:movie/query";
        }
        return null;
    }
}
